use atlas
go
INSERT INTO mon400_data 
 (mon400_data.hostname_id,
  mon400_data.datetime_in,
  mon400_data.msg_kind,
  mon400_data.msg_category,
  mon400_data.msg_size,
  mon400_data.from_abonent_id,
  mon400_data.to_abonent_id,
  mon400_data.msg_id,
  mon400_data.alias_sender_id,
  mon400_data.msg_cirkul,
  mon400_data.msg_errorcode_id
 )
 (SELECT h.id, 
       tmp.datetime_in, 
       tmp.msg_kind, 
       tmp.msg_category, 
       tmp.msg_size,
       a1.id,
       a2.id,
       tmp.msg_id,
       al.id,
       tmp.msg_cirkul,
       e.id     
FROM monitor400_tmp tmp
  INNER JOIN mon400_hostnames h ON tmp.hostname = h.item
  INNER JOIN mon400_abonents a1 ON tmp.from_surname = a1.surname
  INNER JOIN mon400_abonents a2 ON tmp.to_surname = a2.surname
  INNER JOIN mon400_aliases al ON tmp.alias_sender = al.item
  INNER JOIN mon400_errorcodes e ON tmp.msg_errorcode = e.item)
go